//
//  STDSGlassTileEffect.h
//  STDSRenderSDKIOS
//
//  Created by 陈学明 on 2023/6/5.
//

#ifndef STDSGlassTileEffect_h
#define STDSGlassTileEffect_h

#include "STDSBaseEffect.hpp"

class STDSGlassTileEffect: public STDSBaseEffect {
public:
    STDSGlassTileEffect() {
        mEffectType = STDSEffectTypeGlassTile;
    }
    ~STDSGlassTileEffect() {
//        printf("dealloc--STDSMosaicEffect\n");
    }
    
    void initGL() override {
        STDSBaseEffect::initGL();
    }
    
    void setFloatArguments(const char *name, float *floatValue, int n) override {
        if (strcmp(name, "mNumber") == 0) {
            mNumber = *floatValue;
            if (mNumber < 1.0) {
                mNumber = 1.0;
            }
        } else if (strcmp(name, "mIntencity") == 0) {
            mIntencity = *floatValue;
        }
    }
    
public:
    shared_ptr<STDSShaderProgram> getEffectProgram() {
        if (mGlassTileProgram == nullptr) {
            mGlassTileProgram = mContext->getProgram(kShaderSRCGlassTile);
        }
        return mGlassTileProgram;
    }
    
    void draw() override {
        auto program = getEffectProgram();
        if (program == nullptr) {
            return;
        }
        if (mTexture == nullptr) {
            return;
        }
        
        program->use();
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, mTexture->getId());
        program->setUniform(program->getUniform("uTexture"), 0);
        
        program->setUniform(program->getUniform("GRID_SIZE"), mNumber);
        program->setUniform(program->getUniform("uIntencity"), float(mIntencity*20.0));
        program->setUniform2f(program->getUniform("uSize"), float(mSizeSRC.x), float(mSizeSRC.y));
        
        mAtbRect.setPosVex(program->getAttrib("aPos"));
        mAtbRect.setPosTex(program->getAttrib("aTexCoord"));
        mAtbRect.draw();
        program->unuse();
    }

private:
    float mNumber{1.0}; // 数量, 0~20
    float mIntencity{1.0}; // 强度,0.0~1.0
    shared_ptr<STDSShaderProgram> mGlassTileProgram{nullptr};
};

#endif /* STDSGlassTileEffect_h */
